package tron

import (
	"fmt"
	"math"
	"math/big"
	"raymond/global"
)

// https://tronprotocol.github.io/documentation-zh/

const TronApiKey string = "927b860f-f239-4f9f-a1b5-704ec2080b8c"

type Unit int

// 1 trx = 1_000_000 sun
const (
	TRX Unit = iota
	SUN
)

var units = []string{"TRX", "SUN"}

func (u Unit) String() string {
	return units[u]
}

func (u Unit) ToSun(v interface{}) *big.Int {
	var amount *big.Int
	switch u {
	case TRX:
		amount = toSun(v, 6)
	case SUN:
		amount = toSun(v, 0)
	}
	return amount
}

func (u Unit) ToTrc(v interface{}) string {
	var amount *big.Float
	switch u {
	case TRX:
		amount = toDecimal(v, 0)
	case SUN:
		amount = toDecimal(v, 6)
	}
	return fmt.Sprintf("%.6f", amount)
}

// ToDecimal sun to decimals
func toDecimal(ivalue interface{}, decimals int) *big.Float {
	value := new(big.Int)
	switch v := ivalue.(type) {
	case string:
		value.SetString(v, 10)
	case *big.Int:
		value = v
	}

	amount, _ := value.Float64()
	result := new(big.Float).Quo(big.NewFloat(amount), big.NewFloat(math.Pow10(decimals)))

	return result
}

func toSun(iamount interface{}, decimals int) *big.Int {
	amount := new(big.Float)
	switch v := iamount.(type) {
	case string:
		amount.SetString(v)
	case float32:
		amount.SetFloat64(float64(v))
	case float64:
		amount.SetFloat64(v)
	case int64:
		amount.SetInt64(v)
	case int:
		amount.SetInt64(int64(v))
	default:
		// 0
	}

	//mul := decimal.NewFromFloat(float64(10)).Pow(decimal.NewFromFloat(float64(decimals)))
	//result := amount.Mul(mul)
	// 10的6次方
	// tenPow6 := new(big.Int).Exp(big.NewInt(10), big.NewInt(6), nil)

	result := new(big.Float).Mul(amount, big.NewFloat(math.Pow10(decimals)))
	sun := new(big.Int)
	sun.SetString(fmt.Sprintf("%f", result), 10)
	return sun
}

const TRC_TEST_GRPC_URL string = "grpc.nile.trongrid.io:50051"
const TRC_MAIN_GPRC_URL string = "grpc.trongrid.io"
const TRC_TEST_API_URL string = "https://nile.trongrid.io/wallet/"
const TRC_MAIN_API_URL string = "https://api.nileex.io/"
const TRC_TEST_EVENT_URL string = "https://nile.trongrid.io/"
const TRC_MAIN_EVENT_URL string = "https://trongrid.io/"

func GetTrcApiRpcUrl() string {
	switch global.DEFAULT_ENV {
	case global.DEV:
		return TRC_TEST_API_URL
	case global.TEST:
		return TRC_TEST_API_URL
	case global.UAT:
		return TRC_TEST_API_URL
	case global.PROD:
		return TRC_MAIN_API_URL
	}
	return TRC_MAIN_API_URL
}

func GetTrcApiEventUrl() string {
	switch global.DEFAULT_ENV {
	case global.DEV:
		return TRC_TEST_EVENT_URL
	case global.TEST:
		return TRC_TEST_EVENT_URL
	case global.UAT:
		return TRC_TEST_EVENT_URL
	case global.PROD:
		return TRC_MAIN_EVENT_URL
	}
	return TRC_MAIN_EVENT_URL
}

var trcDevContract = []string{"TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj", "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"}
var trcTestContract = []string{"TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj", "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"}
var trcUatContract = []string{"TXLAQ63Xg1NAzckPwKHvzw7CSEmLMEqcdj", "TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"}
var trcMainContract = []string{"TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf"}

func GetTrc20ContractList() []string {
	switch global.DEFAULT_ENV {
	case global.DEV:
		return trcDevContract
	case global.TEST:
		return trcTestContract
	case global.UAT:
		return trcUatContract
	case global.PROD:
		return trcMainContract
	}
	return trcMainContract
}
